package cn.ithers.blog.controller.admin;

import cn.ithers.blog.annotion.CheckTooFrequentCommit;
import cn.ithers.blog.dto.CategoryDTO;
import cn.ithers.blog.pojo.Category;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 13:37
 * @description: 分类Api
 */
@Slf4j
@Api(value = "分类", tags = "分类")
@RestController
@RequestMapping("/admin/category")
public class CategoryAdminApi {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 添加分类
     * 需要管理员权限
     *
     * @param categoryDTO
     * @return
     */
    @CheckTooFrequentCommit
    @ApiOperation("添加文章分类,需要管理里员权限")
    @PreAuthorize("@permission.admin()")
    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.addCategory(categoryDTO);
    }

    /**
     * 删除分类
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("删除文章分类,需要管理里员权限")
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("{categoryId}")
    public ResponseResult deleteCategory(@ApiParam(value = "分类ID", required = true) @PathVariable("categoryId") String categoryId) {
        return categoryService.deleteCategory(categoryId);
    }

    /**
     * 修改分类
     *
     * @param categoryId
     * @param categoryId
     * @return
     */
    @CheckTooFrequentCommit
    @ApiOperation("修改文章分类,需要管理里员权限")
    @PreAuthorize("@permission.admin()")
    @PutMapping("{categoryId}")
    public ResponseResult updateCategory(@ApiParam(value = "分类ID", required = true) @PathVariable("categoryId") String categoryId,
                                         @RequestBody CategoryDTO categoryDTO) {
        return categoryService.updateCategory(categoryId, categoryDTO);
    }


    /**
     * 获取分类
     * 使用的case；
     * 修改的时候获取一下，填充弹框
     * 不获取也可以，从列表中获取
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("获取文章分类,需要管理里员权限")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/{categoryId}")
    public ResponseResult getCategory(@ApiParam(value = "分类ID", required = true) @PathVariable("categoryId") String categoryId) {
        return categoryService.getCategory(categoryId);
    }

    /**
     * 获取分类列表
     * 需要管理员权限
     *
     * @return
     */
    @ApiOperation("获取文章分类列表,需要管理里员权限")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list")
    public ResponseResult listCategories() {
        return categoryService.getCategoryList();
    }
}
